home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / web / noweb / src / c / markup.c < prev    next >
C/C++ Source or Header  |  1995-02-24  |  4KB  |  147 lines

  1. #line 24 "markup.nw"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <assert.h>
  7. #include "markup.h"
  8. #include "strsave.h"
  9. #include "errors.h"
  10.  
  11. #line 60 "markup.nw"
  12. char at_sign = '@';             /* should be the only place '@' is mentioned */
  13. #line 85 "markup.nw"
  14. static char def_marker[] = " %def ";
  15. #define def_length (6)
  16.  
  17. #line 71 "markup.nw"
  18. int starts_doc(char *line) {
  19.     return (*line==at_sign && (line[1]=='\0' || isspace(line[1])));
  20. }
  21.  
  22. char *first_doc_line(char *line) {
  23.     if (line[1]!='\0' && line[1] !='\n') return line+2;
  24.     else return line+1;
  25. }
  26. #line 88 "markup.nw"
  27. int is_def(char *line) {
  28.     
  29. #line 97 "markup.nw"
  30. { static int checked = 0;
  31.   if (!checked) {
  32.     assert(strlen(def_marker) == def_length);
  33.     checked = 1;
  34.   }
  35. }
  36. #line 90 "markup.nw"
  37.     return (*line==at_sign && !strncmp(line+1, def_marker, def_length));
  38. }
  39.  
  40. char *remove_def_marker(char *line) {
  41.     return line+1+def_length;
  42. }
  43. #line 126 "markup.nw"
  44. char *mod_start(char *s, int mark) {
  45.     return find_escaped(s,"<<","@<<", mark);
  46. }
  47. char *mod_end(char *s, int mark) {
  48.     return find_escaped(s,">>","@>>", mark);
  49. }
  50. #line 141 "markup.nw"
  51. int starts_code (char *line, char *filename, int lineno) {
  52.     char *tail;
  53.     if (mod_start(line,0) != line+2) return 0;
  54.     tail = mod_end(line+2,0);
  55.     if (tail == NULL) 
  56. #line 164 "markup.nw"
  57. {
  58.     errorat(filename, lineno, Error, "Module name doesn't end", line);
  59.     return 0;
  60. }
  61. #line 146 "markup.nw"
  62.     if (*tail++ != '=') return 0;
  63.     while (isspace(*tail)) tail++;
  64.     return (*tail == '\0');
  65. }
  66.  
  67. void getmodname(char *dest, int size, char *source) {
  68.     /* excess characters in the module name are ignored */
  69.     char *p = strsave(source);
  70.     char *q = mod_start(p,1);
  71.  
  72.     if (q==NULL) 
  73. #line 169 "markup.nw"
  74. {
  75.     free(p);
  76.     impossible
  77.         ("I couldn't manage to extract a module name, but I'm sure I saw one");
  78. }
  79. #line 157 "markup.nw"
  80.     if (mod_end(q,1)==NULL) 
  81. #line 169 "markup.nw"
  82. {
  83.     free(p);
  84.     impossible
  85.         ("I couldn't manage to extract a module name, but I'm sure I saw one");
  86. }
  87. #line 158 "markup.nw"
  88.     strncpy(dest,q,size-1);
  89.     dest[size-1] = '\0';
  90.     free(p);
  91. }
  92. #line 181 "markup.nw"
  93. char *quote_start(char *s, int mark) {
  94.     return find_escaped(s,"[[",NULL, mark);
  95. }
  96. #line 188 "markup.nw"
  97. char *quote_end(char *s, int mark) {
  98.     char *t = find_escaped(s, "]]", NULL, 0);
  99.     if (t == NULL) 
  100.         return t;
  101.     else {
  102.         t -= 2;         /* subtract length of ]] */
  103.         assert(t[0] == ']' && t[1] == ']');
  104.         while (t[2] == ']') t++;
  105.         assert(t[0] == ']' && t[1] == ']');
  106.         if (mark) *t = 0;
  107.         return t+2;
  108.     }
  109. }
  110. #line 220 "markup.nw"
  111. char *find_escaped(register char *s, char *search, char *escape, int mark) {
  112.     register char first = *search;
  113.     register char first_escape = (escape != NULL ? *escape : '\0');
  114.     int searchlen = strlen(search);
  115.     int escapelen = (escape != NULL ? strlen(escape) : 0);
  116.  
  117.     do {
  118.         while (*s && *s != first && *s != first_escape) s++;
  119.         if (!*s) break;
  120.         if (first_escape && !strncmp(s,escape,escapelen)) {
  121.                 s += escapelen;
  122.                 continue;
  123.         }
  124.         if (!strncmp(s,search,searchlen)) break;
  125.         s++;
  126.     } while (*s != '\0');
  127.     /* here either s is empty or it points to the first unescaped [[search]] */
  128.     if (*s == '\0') return NULL;
  129.     if (mark) *s = '\0';
  130.     return s+searchlen;
  131. }
  132. #line 253 "markup.nw"
  133. char *unescape (char *s) {
  134.     char *t = malloc(strlen(s)+1);
  135.     char *r = t;
  136.     checkptr(t);
  137.     while (*s != '\0') {
  138.         if (*s == at_sign) {
  139.             if (!strncmp(s+1,"<<",2) || !strncmp(s+1,">>",2)) s++;
  140.         }
  141.         *t++ = *s++;
  142.     }
  143.     *t = '\0';
  144.  
  145.     return r;
  146. }
  147.